package com.qfedu.edu.finishorderservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.edu.bo.OrderShow;
import com.qfedu.edu.bo.QueueMessage;
import com.qfedu.edu.bo.UpdateOrderStatus;
import com.qfedu.edu.constance.RedisConstance;
import com.qfedu.edu.enum1.BuyStatus;
import com.qfedu.edu.finishorderservice.mapper.OrderDetailMapper;
import com.qfedu.edu.manager.RedisManager;
import com.qfedu.edu.po.Order;
import com.qfedu.edu.finishorderservice.mapper.OrderMapper;
import com.qfedu.edu.finishorderservice.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.edu.po.OrderDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaobobo
 * @since 2024-11-11
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private RedisManager redisManager;

    @Transactional
    @Override
    public void createOrder(String receiveMessage) {
        //首先是找到订单的对象
        QueueMessage queueMessage = JSON.parseObject(receiveMessage, QueueMessage.class);
        //然后取出这个消息
        String message = queueMessage.getMessage();
        //这里写if的意思是为了解决队列消息乱序的问题
        if(queueMessage.getType()==1){
            //说明是下订单
            OrderShow order = JSON.parseObject(message, OrderShow.class);
            order.setPaymentType(0);
            order.setPostFee(0L);
            //接下来就可以下订单了...
            orderMapper.insert(order);
            //接下来还需要向订单详情表写入数据
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(order.getOrderId());
            orderDetail.setImage(order.getPhoto());
            orderDetail.setNum(1);
            orderDetail.setClassId(order.getClassId());
            orderDetail.setPrice(order.getActualFee().intValue());
            orderDetail.setTitle(order.getClassName());
            //接下来再将这个数据放到数据库中
            orderDetailMapper.insert(orderDetail);
            log.info("订单号:{}下订单完成",order.getOrderNum());
        }else{
            //TODO 代表的是要去修改订单状态了...
            //将队列的消息转换成对象...
            UpdateOrderStatus updateOrderStatus = JSON.parseObject(message, UpdateOrderStatus.class);
            //接下来获取订单号以及 我们的这个订单的状态
            String orderNumber = updateOrderStatus.getOrderNumber();
            Integer status = updateOrderStatus.getStatus();
            //接下来去修改这个订单的状态...
            //首先应该是查询这个订单
            QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
            orderQueryWrapper.eq("order_num",orderNumber);
            Order order = orderMapper.selectOne(orderQueryWrapper);
            //接下来去修改这个订单状态更新
            order.setStatus(status);
            //然后更新这个数据库
            orderMapper.updateById(order);
        }
    }

    @Override
    public void orderStatusCheck(String val) {
        //接下来先将数据弄成对象
        QueueMessage queueMessage = JSON.parseObject(val, QueueMessage.class);
        //然后：获取数据
        String message = queueMessage.getMessage();
        OrderShow orderShow = JSON.parseObject(message, OrderShow.class);
        //接下来就可以去查询数据了
        String orderNum = orderShow.getOrderNum();
        //去Redis中查询这个订单的支付状态...
        //首先构建key
        String key=RedisConstance.getPayStatusPrefix() + orderNum;
        //接下来去Redis中查询数据
        Integer status = (Integer) redisManager.getObject(key);
        if(null==status){
            //说明这个状态都还没有写
            //说明也没有支付...
            //说明支付失败 那么库存需要还原
            String key1=RedisConstance.getBuyCountPrefix()+orderShow.getClassId();
            //将这个状态-1就可以
            redisManager.incr(key1,-1);
            //秒杀状态:
            String key2=RedisConstance.getBuyStatusPrefix()+orderShow.getUserId()+":"+orderShow.getClassId();
            //接下来向Redis中写入数据
            redisManager.addObject(key2, BuyStatus.FAIL.getStatus(),60, TimeUnit.DAYS);
        }
        //判断这个状态是不是支付
        if(status==1){
            //说明支付失败 那么库存需要还原
            String key1=RedisConstance.getBuyCountPrefix()+orderShow.getClassId();
            //将这个状态-1就可以
            redisManager.incr(key1,-1);
        }
    }
}
